package AI.Enemy;

import java.awt.Graphics;
import java.util.Collections;
import java.util.Random;

import AI.AiEntity;
import AI.Crew.Crew;
import FrameWork.PathFinding.Step;
import Main.SpaceGame;
import Tasks.Attack;
import Tasks.Movement;
import Tasks.Task;
import Tasks.UrgencyComparator;
import World.World;

public abstract class Enemy extends AiEntity
{

	public Enemy(int x, int y, SpaceGame main) 
	{
		super(x, y, main,100);
		this.x = x;
		this.y = y;
		this.main = main;
		speed = .5;
	}
	
	@Override
	public abstract void draw(Graphics g);

	@Override
	public int getTileWidth() 
	{
		return World.worldXSize;
	}

	@Override
	public int getTileHeight() 
	{
		return World.worldYSize;
	}

	@Override
	public boolean getSolid(int x, int y) 
	{
		if(x < World.worldXSize && y < World.worldYSize)
			return main.world.tiles[x][y].isSolid();
		else
			return true;
	}

	@Override
	public int getWeight(int x, int y) 
	{
		return 1;
	}
	
	public void updateTask()
	{
		/**
		{
			if(currentTask == null)
			{
				if(taskList.size() <= 0)
				{ 
					for(int i = 0;i<main.aiManager.entityList.size();i++)
					{
						AiEntity current = main.aiManager.entityList.get(i);
						if(current instanceof Crew && taskList.size() <= 0 && currentTask == null)
						{
							if(canReachPos(current.getX(), current.getY(), Math.abs(current.getX()-x)+Math.abs(current.getY()-y)*2));
							{
								System.out.println("Added");
								taskList.add(new Attack(current));
							}
						}
					}
				}
				if(taskList.size()>0)
				{
					Collections.sort(taskList,new UrgencyComparator());
					currentTask = taskList.get(0);
				}
			}
			else
			{
				checkForBetterTask(currentTask);
				currentTask.performTask(this);
				if(currentTask.isFinished(this))
				{
					taskList.remove(currentTask);
					currentTask = null;
				}
			}
		}
		**/
		if(currentTask == null)
		{
			Random rand = new Random();
			int dx = rand.nextInt(3)-1, dy = rand.nextInt(3)-1;
			
			if(currentTask == null && !main.world.tiles[x+dx][y+dy].isSolid())
			currentTask = new Movement(x+dx, y+dy);
		}
		else
		{
			currentTask.performTask(this);
			if(currentTask.isFinished(this))
			{
				taskList.remove(currentTask);
				currentTask = null;
			}
		}
	}
	
	public void checkForBetterTask(Task currentTask)
	{
		for(Task t:taskList)
		{
			if (t.urgency > currentTask.urgency)
			{
				currentTask.finished = true;
				taskList.remove(currentTask);
				currentTask = t;
			}
		}
	}
}
